package com.wslds.blog.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wslds.base.AbstractService;
import com.wslds.base.util.Collects;
import com.wslds.bean.Article;
import com.wslds.bean.ext.ArticleExt;

@Transactional
@Service("articleService")
public class ArticleService extends AbstractService<Article> {


	public List<ArticleExt> getNei( ArticleExt articleExt ){
		
		List<ArticleExt> neiList = dao.findExt("article_nei_bk", articleExt);
		if( Collects.notNull(neiList)){
			if( neiList.size() == 1 ){
				Long neiId = neiList.get(0).getId();
				if( articleExt.getId() < neiId ){
					neiList.add(new ArticleExt());
					Collections.reverse(neiList);
				}else{
					neiList.add(new ArticleExt());
				}
			}
		}else{
			neiList = new ArrayList<ArticleExt>();
			neiList.add(new ArticleExt());
			neiList.add(new ArticleExt());
		}
		
		for( ArticleExt ext: neiList ){
			if( null != ext.getId() ){
				if( ext.getTitle().length() > 11 ){
					ext.setTitle(ext.getTitle().substring(0,11) + "...");
				}
			}
		}
		
		return neiList;
	}
	
	public List<ArticleExt> getNeiWeb( ArticleExt articleExt ){
		
		List<ArticleExt> neiList = dao.findExt("article_nei", articleExt);
		if( Collects.notNull(neiList)){
			if( neiList.size() == 1 ){
				Long neiId = neiList.get(0).getId();
				if( articleExt.getId() < neiId ){
					neiList.add(new ArticleExt());
				}else{
					neiList.add(new ArticleExt());
					Collections.reverse(neiList);
				}
			}
		}else{
			neiList = new ArrayList<ArticleExt>();
			neiList.add(new ArticleExt());
			neiList.add(new ArticleExt());
		}
		
		for( ArticleExt ext: neiList ){
			if( null != ext.getId() ){
				if( ext.getTitle().length() > 11 ){
					ext.setTitle(ext.getTitle().substring(0,11) + "...");
				}
			}
		}
		
		return neiList;
	}
	
	
	
}
